What is `objc_msgSend_fixup`, exactly?
Posted
by
Luis Antonio Botelho O. Leite
on Stack Overflow
See other posts from Stack Overflow
or by Luis Antonio Botelho O. Leite
Published on 2012-09-01T13:55:48Z
Indexed on
2012/09/01
15:38 UTC
Read the original article
Hit count: 329
I'm messing around with the Objective-C runtime, trying to compile objective-c code without linking it against libobjc
, and I'm having some segmentation fault problems with a program, so I generated an assembly file from it. I think it's not necessary to show the whole assembly file. At some point of my main
function, I've got the following line (which, by the way, is the line after which I get the seg fault):
callq *l_objc_msgSend_fixup_alloc
and here is the definition for l_objc_msgSend_fixup_alloc
:
.hidden l_objc_msgSend_fixup_alloc # @"\01l_objc_msgSend_fixup_alloc"
.type l_objc_msgSend_fixup_alloc,@object
.section "__DATA, __objc_msgrefs, coalesced","aw",@progbits
.weak l_objc_msgSend_fixup_alloc
.align 16
l_objc_msgSend_fixup_alloc:
.quad objc_msgSend_fixup
.quad L_OBJC_METH_VAR_NAME_
.size l_objc_msgSend_fixup_alloc, 16
I've reimplemented objc_msgSend_fixup
as a function (id objc_msgSend_fixup(id self, SEL op, ...)
) which returns nil
(just to see what happens), but this function isn't even being called (the program crashes before calling it).
So, my question is, what is callq *l_objc_msgSend_fixup_alloc
supposed to do and what is objc_msgSend_fixup
(after l_objc_msgSend_fixup_alloc:
) supposed to be (a function or an object)?
Edit
To better explain, I'm not linking my source file against the objc library. What I'm trying to do is implement some parts of the libray, just to see how it works. Here is an approach of what I've done:
#include <stdio.h>
#include <objc/runtime.h>
@interface MyClass {
}
+(id) alloc;
@end
@implementation MyClass
+(id) alloc {
// alloc the object
return nil;
}
@end
id objc_msgSend_fixup(id self, SEL op, ...) {
printf("Calling objc_msgSend_fixup()...\n");
// looks for the method implementation for SEL in self's vtable
return nil; // Since this is just a test, this function doesn't need to do that
}
int main(int argc, char *argv[]) {
MyClass *m;
m = [MyClass alloc]; // At this point, according to the assembly code generated
// objc_msgSend_fixup should be called. So, the program should, at least, print
// "Calling objc_msgSend_fixup()..." on the screen, but it crashes before
// objc_msgSend_fixup() is called...
return 0;
}
If the runtime needs to access the object's vtable to find the correct method to call, what is the function which actually does this? I think it is objc_msgSend_fixup
, in this case. So, when objc_msgSend_fixup
is called, it receives an object as one of its parameters, and, if this object hasn't been initialized, the function fails.
So, I've implemented my own version of objc_msgSend_fixup
. According to the assembly source above, it should be called. It doesn't matter if the function is actually looking for the implementation of the selector passed as parameter. I just want objc_msgSend_lookup
to be called. But, it's not being called, that is, the function that looks for the object's data is not even being called, instead of being called and cause a fault (because it returns a nil
(which, by the way, doesn't matter)). The program seg fails before objc_msgSend_lookup
is called...
© Stack Overflow or respective owner